/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import ScreenLayouts.ScreenLayout;
import graphics.RenderInstruction;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Arrays;
import stealth.Main;

/**
 * TODO: Implement GUI class Javadoc
 * @author Joshua Mabrey
 */
public class GUI {

    public GUI() {
        synchronized (itemLock) {
            this.items = new ArrayList<GUIItem>();
        }
    }
    public final Object itemLock = new Object();
    int mousex, mousey;
    boolean clicked;
    protected ArrayList<GUIItem> items;

    public void addItems(ScreenLayout layout) {
        synchronized (itemLock) {
            this.items.addAll(Arrays.asList(layout.getItems()));
        }
    }

    public void clear() {
        synchronized (itemLock) {
            this.items.clear();
        }
    }

    public void update() {
        mousex = Main.game.getMouseX();
        mousey = Main.game.getMouseY();


        boolean clicking = Main.game.click();

        synchronized (itemLock) {
            if (items.size() < 1) {
                return;
            }
        }
        for (GUIItem item : (ArrayList<GUIItem>)items.clone()) {
            synchronized (itemLock) {
                item.reactToHover(mousex, mousey);


                if (clicking) {
                    item.reactToClick(mousex, mousey);


                }
            }
        }
    }

    public RenderInstruction render() {
        return new GUIRender(this, mousex, mousey);
    }
}

class GUIRender
        extends RenderInstruction {

    private int mousex, mousey;
    private GUI m;

    public GUIRender(GUI m, int mousex, int mousey) {
        this.mousex = mousex;
        this.mousey = mousey;
        this.m = m;
    }

    @Override
    public void render(Graphics2D g) {
        synchronized (m.itemLock) {
            for (GUIItem item : m.items) {
                if (item instanceof Button) {
                    drawButton(g, (Button) item, Color.WHITE, Color.BLACK);
                } else if (item instanceof Title) {
                    drawTitle(g, (Title) item, Color.WHITE, Color.RED);
                } else if (item instanceof HotKeyProvider) {
                    ((HotKeyProvider) item).reactToKeyEvents();
                    /**
                     * I debated putting this code statement in. AFter all, hotkey updates don't
                     * belong in the render method. However I placed it here anyway, simply
                     * because it was easier.
                     */
                } else if (item instanceof Line) {
                    drawLine(g, (Line) item, Color.BLACK);
                }
            }
        }


    }

    private void drawButton(Graphics2D g, Button b, Color fore, Color back) {

        int stringWidth = g.getFontMetrics().stringWidth(b.text);
        int stringHeight = g.getFontMetrics().getHeight();
        if (stringWidth > b.width) {
            b.width = stringWidth;
        }
        if (stringHeight > b.height) {
            b.height = stringHeight;
        }
        if (!b.filled) {
            g.setColor(Color.BLACK);
            g.drawRect(b.x, b.y, b.width, b.height);
            float centerWidthOfText = stringWidth / 2;
            float centerWidthOfButton = b.width / 2;
            float centerHeightOfText = stringWidth / 2;
            float centerHeightOfButton = b.height / 2;
            float correctedTextX = b.x + (centerWidthOfButton - centerWidthOfText);
            float correctedTextY = b.y + centerHeightOfButton;
            g.drawString(b.text, correctedTextX, correctedTextY);
            g.setColor(back);
        } else {
            g.setColor(back);
            g.fillRect(b.x, b.y, b.width, b.height);
            float centerWidthOfText = stringWidth / 2;
            float centerWidthOfButton = b.width / 2;
            float centerHeightOfText = stringWidth / 2;
            float centerHeightOfButton = b.height / 2;
            float correctedTextX = b.x + (centerWidthOfButton - centerWidthOfText);
            float correctedTextY = b.y + centerHeightOfButton;
            g.setColor(fore);
            g.drawString(b.text, correctedTextX, correctedTextY);
            g.setColor(back);
        }
    }

    private void drawTitle(Graphics2D g, Title t, Color fore, Color back) {
        g.setColor(back);
        if (t.font != null) {
            g.setFont(t.font);
            g.drawString(t.text, t.x, t.y);
        } else {
            g.drawString(t.text, t.x, t.y);
        }

    }

    private void drawLine(Graphics2D g, Line l, Color fore) {
        if (l.vertical) {
            g.drawLine(l.x, l.y, l.x, l.y + l.length);
        } else {
            g.drawLine(l.x, l.y, l.x + l.length, l.y);
        }
    }
}
